package me.support.model;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import me.support.enums.ResultCode;

import java.util.Date;


/**
 * @author felix
 * @date 2022/1/9/0009
 */

@Data
public final class CommonResult<T> {

    private int code;

    private String msg;

    private T data;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date timestamp;

    public CommonResult() {
        this.timestamp = new Date();
    }

    public static <T> CommonResult<T> success(T data) {
        CommonResult<T> resultData = new CommonResult<>();
        resultData.setCode(ResultCode.SUCCESS.getCode());
        resultData.setMsg(ResultCode.SUCCESS.getMessage());
        resultData.setData(data);
        return resultData;
    }

    public static <T> CommonResult<T> fail(int code, String message) {
        CommonResult<T> resultData = new CommonResult<>();
        resultData.setCode(code);
        resultData.setMsg(message);
        return resultData;
    }

}
